package basedonnee;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import classe.ConnexionUserFrame;

public class DBManager {
	
	private static Connection connection;
	Statement st;
	
	public Object oRestbCnx;
	
	public DBManager(){
		setConnection(null);
		st = null;
	    //ResultSet rs = null;

		try {
 
			Class.forName("org.postgresql.Driver");
 
		} catch (ClassNotFoundException e) {
 
			System.out.println("Where is your PostgreSQL JDBC Driver? "
					+ "Include in your library path!");
			e.printStackTrace();
			return;
 
		}
		try {
			 
			setConnection(DriverManager.getConnection(
					"jdbc:postgresql://localhost:5432/Gestion_Recommandation", "dida",
					"dida"));
 
		} catch (SQLException e) {
 
			System.out.println("Erreur de connection au serveur de la base de donnee! ");
			e.printStackTrace();
		}
	}
 
		
		public void insertFonction(){
			if (getConnection() != null) {
				System.out.println("Connection reussie!");
				  /*try{
			      st = connection.createStatement();
						//rs = st.executeQuery("SELECT * FROM \"Utilisateur\"");
			      String queryplat = "INSERT INTO \"Plat\" ( \"ID_Plat\", \"Nom_Plat\", " 
							+ "\"Categ_Plat\", \"Descrip_Plat\")" 
							+ " VALUES (nextval('seqplat'), 'Poulet', 'Traditionnel', 'Poulet a la brise')";
			      int r = st.executeUpdate(queryplat);
			      System.out.println(r);
			      rs = st.executeQuery("SELECT * FROM \"Plat\"");
						// if (rs.next()) {
			              //  System.out.println(rs.getString("Nom_User"));
			           // }
			      while (rs.next()String queryplat = "INSERT INTO \"Plat\" ( \"ID_Plat\", \"Nom_Plat\", " 
							+ "\"Categ_Plat\", \"Descrip_Plat\")" 
							+ " VALUES (nextval('seqplat'), 'Poulet', 'Traditionnel', 'Poulet a la brise')";
			      int r = st.executeUpdate(queryplat);
			      System.out.println(r);) {
//		                System.out.println(rs.getString("ID_User")+ " " + rs.getString("Nom_User")+ 
//		                		" " + rs.getString("Prenom_User"));
						System.out.println(rs.getString(1) + " " +  rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
		            }
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

			} else {
				System.out.println("Erreur de connection!");
			}
			*/
			
		}
 
		
	}
    public ConnexionUserFrame existUser(String _login, String _mdp){
    	ConnexionUserFrame bUser = null;
    	String zQueryUser = "SELECT * FROM \"Utilisateur\" where \"Login_User\"='"+_login+"' and \"Pass_Word_User\" = '"+_mdp+"'";
    	System.out.println(zQueryUser);
		try
		{
			Statement oStmtUser = (Statement)this.getConnection().createStatement () ;	
			ResultSet oRsUser = (ResultSet) oStmtUser.executeQuery (zQueryUser) ;
			if (oRsUser.next ())
			{
				bUser = new ConnexionUserFrame();
				bUser.setIdUser(oRsUser.getInt("ID_User"));
				bUser.setNomUser(oRsUser.getString("Nom_User"));
				bUser.setLoginUser(oRsUser.getString("Login_User"));
				bUser.setPwdUser(oRsUser.getString("Pass_Word_User"));
				
			}
		}
		catch (SQLException _e)
		{
			System.out.println ("Erreur interne pendant la requete !") ;
			_e.printStackTrace () ;
		}
    	return bUser;
    }


	public static Connection getConnection() {
		return connection;
	}


	public static void setConnection(Connection connection) {
		DBManager.connection = connection;
	}
    
	}

